package pl.register.android.activities.tasks;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
import pl.register.android.R;
import pl.register.android.task.Task;

/**
 * @author Mateusz Strzelecki
 * @since 2013-05-02
 */

public class TaskDisplayFragment extends SherlockFragment {

    private TextView tvCreationDate;

    private TextView tvTitle;

    private TextView tvMessage;

    private Task displayTask;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View contentLayout = inflater.inflate(R.layout.task_details_fragment_layout, container, false);
        registerViews(contentLayout);

        return contentLayout;
    }

    private void registerViews(View content) {
        tvCreationDate = (TextView) content.findViewById(R.id.task_creation_date_text_view);
        tvTitle = (TextView) content.findViewById(R.id.task_title_text_view);
        tvMessage = (TextView) content.findViewById(R.id.task_message_text_view);
    }

    public void settleDisplayTask(Task task) {
        displayTask = task;
        refresh();
    }

    public void refresh() {
        if (displayTask != null) {
            tvCreationDate.setText(displayTask.getCreationDateInString());
            tvTitle.setText(displayTask.getTitle());
            tvMessage.setText(displayTask.getMessage());
        } else {
            tvCreationDate.setText("");
            tvTitle.setText("");
            tvMessage.setText("");
        }
    }

    public void removedTask(Task removedTask) {
        if (removedTask.equals(displayTask)) {
            displayTask = null;
            refresh();
        }
    }

}